package com.jake.xpage.page;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;

public class DefaultPageConfigHandler implements IXPageConfigHandler {
    @Override
    public void configPage(@NonNull XPage page, @NonNull XPageConfig config) {
        @NonNull Activity activity = (Activity) page.getContext();
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        WindowInsetsControllerCompat controllerCompat = WindowCompat.getInsetsController(window, decorView);
        if (config.statusBarShow) {
            int type = WindowInsetsCompat.Type.statusBars();
            if (config.navigationBarShow) {
                type |= WindowInsetsCompat.Type.navigationBars();
            }
            controllerCompat.show(type);
            controllerCompat.setAppearanceLightStatusBars(config.statusBarDark);
            controllerCompat.setAppearanceLightNavigationBars(config.navigationBarDark);
            if (config.immersionStatusBar) {
                int flags = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                window.getDecorView().setSystemUiVisibility(flags);
                window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            } else {
                int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                window.getDecorView().setSystemUiVisibility(flags);
            }
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                window.setNavigationBarColor(config.navigationBarColor);
                window.setStatusBarColor(config.statusBarColor);
            }
        } else {
            int type = WindowInsetsCompat.Type.statusBars();
            if (!config.navigationBarShow) {
                type |= WindowInsetsCompat.Type.navigationBars();
            }
            controllerCompat.hide(type);
            window.addFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            );
            int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(flags);
            if (config.statusBarInCutoutMode && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                window.setAttributes(lp);
            }
        }

    }
}
